package com.lgj.springboot_lgj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgj.springboot_lgj.bean.basic.Category;
import com.lgj.springboot_lgj.dao.basic.CategoryMapper;
import com.lgj.springboot_lgj.dao.ext.CategoryExtMapper;
import com.lgj.springboot_lgj.exception.CustomerException;
import com.lgj.springboot_lgj.service.IArticleService;
import com.lgj.springboot_lgj.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author qjl
 * @Date 2022/6/19
 */
@Slf4j
@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryExtMapper mapper2;
    @Autowired
    private CategoryMapper mapper;
    @Autowired
    private IArticleService art;
    private List<Integer> artid = new ArrayList();
    private List<Integer> ids = new ArrayList();
    private List<Integer> son_ids = new ArrayList();
    private int[] int_category_id = null;



    //通过id查询资讯栏目
    @Override
    public List<Category> findAll() {
        List<Category> list = mapper2.findAllCategory();
        return list;
    }

    //保存（添加）栏目
    @Override
    public void saveCategory(Category category) {
        mapper.insertSelective(category);
    }

    //更新（修改）栏目
    @Override
    public void updateCategory(Category category) {
        mapper.updateByPrimaryKey(category);
    }

    //分页查询栏目
    @Override
    public PageInfo<Category> pageQueryAll(int pageNum, int pageSize) {
        //1.设置分页信息
        PageHelper.startPage(pageNum, pageSize, true);
        //2.执行分页查询
        List<Category> list = mapper2.findAllCategory();
        PageInfo<Category> pageInfo = new PageInfo<>(list);
        //返回数据
        return pageInfo;
    }

    //循环获取子栏目id
    @Override
    public List<Integer> loopDeleteSonById(Integer id) {
        List<Integer> list = mapper2.selectCommentByParentId(id);//查找目标栏目的子栏目
        if (!list.isEmpty()) {//判断是否有子栏目
            for (Integer it : list) {
                ids.add(0, it);
                this.loopDeleteSonById(it);
            }
        }
        return ids;
    }


    //根据栏目编号删除栏目
    @Override
    public void deleteById(Integer id) {
        if (mapper2.findCategoryById(id) != null) {//判断id是否有效
            son_ids = this.loopDeleteSonById(id);//循环获取子栏目id集合0
            son_ids.add(id);//将目标栏目id加入集合
            for (int i = 0; i < son_ids.size(); i++) {
                ids = mapper2.findArtcleIdById(son_ids.get(i));//查找其对应的栏目id
                for (int j = 0; j < ids.size(); j++) {
                    art.deleteById(ids.get(j));
                }//循环删除对应的咨询信息
                mapper.deleteByPrimaryKey(son_ids.get(i));//删除栏目
            }
            son_ids.clear();//清空集合
        } else {
            throw new CustomerException("删除失败，请确认编号无误");
        }

    }

    //批量删除栏目
    @Override
    public String batchDelete(int[] category_id) {
        //判断数组中是否有无效id
        for (int i = 0; i < category_id.length; i++) {
            if (mapper2.findCategoryById(category_id[i]) == null) {
                String meg ="删除失败，请输入正确的id";
                return meg;
            }
        }

        //删除
        for (int m = 0; m < category_id.length; m++) {
            if (mapper.selectByPrimaryKey(category_id[m]) != null){//检查自身是否已被删除
                List<Integer> son_ids = this.loopDeleteSonById(category_id[m]);//循环获取子栏目id集合
                son_ids.add(category_id[m]);//将目标栏目id加入集合
                for (int g = 0; g < son_ids.size(); g++) {
                    ids = mapper2.findArtcleIdById(son_ids.get(g));//查找其对应的栏目id
                    for (int j = 0; j < ids.size(); j++) {
                        art.deleteById(ids.get(j));
                    }//循环删除对应的咨询信息
                    mapper.deleteByPrimaryKey(son_ids.get(g));
                }
                son_ids.clear();//清空集合
            }

        }
        String meg ="删除成功！";
        return meg;
    }
}
